const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    cartlistdata:[],
    selectitem:[],
    totalprice:0,
    totalnums:0,
    allselect:true,
    isloading:true
  },
  onShow: function (option) {
    this.cartlist()
  },
  goshop(){
    wx.switchTab({
      url:"/pages/mall/mall"
    });
  },
  pay(){

    if(app.globalData.isios){
      wx.showToast({
        title:"ios提示不能支付",
        icon:"none"
      })
      return
    }
    if(this.data.selectitem.length<=0){
      wx.showToast({
        title:"请选择商品",
        icon:"none"
      })
      return
    }
    var cartId=[];
    for(var item of this.data.selectitem){
      cartId.push(item.cartId)
    }
    wx.navigateTo({
      url:"/pages/mall/order?id="+cartId.join(',')+"&type=cart"
    });
  },
  cartlist(){
    Controller.cartlist().then(res=>{
      if(res.data.baseData){
        for(let item of res.data.baseData){
          item.select=true
        }
        this.setData({
          cartlistdata:res.data.baseData
        })
        this.totalnums()
      }
      this.setData({
        isloading:false
      })
    })
  },
  mins(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    for(var item of lists){
      if(item.goodsId==id){
        item.amount--;
        item.select=true
        if(item.amount<=0){
          item.amount=1
          return
        }
        selectitem=item
      }
    }

    this.addOrUpdate(selectitem,1,2,lists)
  },
  plus(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    for(var item of lists){
      if(item.goodsId==id){
        item.amount++;
        item.select=true
        if(item.amount>item.stock){
          item.amount=item.stock
          wx.showToast({
            title:"库存不足",
            icon:"none"
          })
          return
        }
        selectitem=item
      }
    }
    this.addOrUpdate(selectitem,1,1,lists)
  },
  changenums(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    var oldnum=0
    var type=1
    var disnums=0
    for(var item of lists){
      if(item.goodsId==id){
        oldnum=item.amount
        item.amount=e.detail.value;
        item.select=true
        if(item.amount<=0){
          item.amount=1
          return
        }
        if(item.amount>item.stock){
          item.amount=item.stock
          this.setData({
            cartlistdata:lists
          })
          wx.showToast({
            title:"库存不足",
            icon:"none"
          })
          return
        }
        selectitem=item
        if(oldnum>item.amount){
          type=2
          disnums=oldnum-item.amount
        }
        if(oldnum<item.amount){
          type=1
          disnums=item.amount-oldnum
        }
      }
    }
    
    this.addOrUpdate(selectitem,disnums,type,lists)
  },
  addOrUpdate(selectitem,num,type,lists){
    var data={
      goodsId:selectitem.goodsId,
      specs:selectitem.specs,
      amount:num,
      type:type
    }
    Controller.addOrUpdate(data).then(res=>{
      this.setData({
        cartlistdata:lists
      })
      this.totalnums()
    })
  },
  deleteCart(selectitem){
    Controller.deleteCart({cartIds:selectitem.cartIds}).then(res=>{
      this.totalnums()
    })
  },
  delete(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否确定删除?',
      success(res) {
        if (res.confirm) {

          for(let i=0;i<lists.length;i++){
            if(lists[i].goodsId==id){
              lists[i].amount=0;
              lists[i].select=false;
              selectitem=lists[i];
              console.log(lists[i].cartId)
              Controller.deleteCart({cartIds:lists[i].cartId}).then(res=>{
              console.log('list==>',i,lists)
                lists.splice(i,1);
                console.log('del list==>',lists)
                that.setData({
                  cartlistdata:lists
                })
                that.totalnums()
              })
            }
          }
        }
      }
    })
    this.totalnums()
  },
  select(e) {
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    for(var item of lists){
      if(item.goodsId==id){
        item.select=!item.select
      }
    }
    this.setData({
      cartlistdata:lists
    })
    this.totalnums()
  },
  allselect(e) {
    var lists=this.data.cartlistdata;
    for(var item of lists){
      item.select=!this.data.allselect
    }
    this.setData({
      cartlistdata:lists,
      allselect:!this.data.allselect
    })
    this.totalnums()
  },
  totalnums(){
    var lists=this.data.cartlistdata;
    var number=0;
    var price=0;
    var selectitem=[]
    for(var item of lists){
      if(item.select){
        selectitem.push(item)
        number+=Number(item.amount);
        price+=Number(item.amount*item.discountPrice)
      }
    }
    this.setData({
      totalprice:price.toFixed(2),
      totalnums:number,
      selectitem
    })
  },
})